package com.bst.web.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bst.common.utils.WeatherUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.bst.common.annotation.Log;
import com.bst.common.enums.BusinessType;
import com.bst.base.domain.BaseWeather;
import com.bst.base.service.IBaseWeatherService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 天气Controller
 * 
 * @author ruoyi
 * @date 2024-03-08
 */
@Controller
@RequestMapping("/base/weather")
public class BaseWeatherController extends BaseController
{
    private String prefix = "base/weather";

    @Autowired
    private IBaseWeatherService baseWeatherService;

    @GetMapping()
    public String weather()
    {
        return prefix + "/weather";
    }

    @GetMapping("/searchData")
    @ResponseBody
    public AjaxResult searchData(String month)
    {
        try {
            JSONArray arr = WeatherUtil.queryWeather(month);
            JSONObject jsonObject;
            List<BaseWeather> weathers = new ArrayList<>();
            BaseWeather weather;
            for(int i=0;i<arr.size();i++) {
                jsonObject = arr.getJSONObject(i);
                weather = new BaseWeather();
                weather.setDayOccur(jsonObject.getString("date"));
                //weather.setGradeWind(jsonObject.get("win_speed").toString());
                weather.setTempMax(Long.parseLong(jsonObject.getString("tem_day")));
                weather.setTempMin(Long.parseLong(jsonObject.getString("tem_night")));
                weather.setWeather(jsonObject.getString("wea_day"));
                weathers.add(weather);
            }
            if(weathers.size()>0) {
                baseWeatherService.insertBatchBaseWeather(weathers);
            }
            return AjaxResult.success(null);
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 查询天气列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseWeather baseWeather)
    {
        startPage();
        List<BaseWeather> list = baseWeatherService.selectBaseWeatherList(baseWeather);
        return getDataTable(list);
    }

    /**
     * 导出天气列表
     */
    @Log(title = "天气", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseWeather baseWeather)
    {
        List<BaseWeather> list = baseWeatherService.selectBaseWeatherList(baseWeather);
        ExcelUtil<BaseWeather> util = new ExcelUtil<BaseWeather>(BaseWeather.class);
        return util.exportExcel(list, "天气数据");
    }

    /**
     * 新增天气
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存天气
     */
    @Log(title = "天气", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    @ResponseBody
    public AjaxResult addBatch(@RequestBody List<BaseWeather> list)
    {
        return toAjax(baseWeatherService.insertBatchBaseWeather(list));
    }

    /**
     * 修改天气
     */
    @GetMapping("/edit/{dayOccur}")
    public String edit(@PathVariable("dayOccur") String dayOccur, ModelMap mmap)
    {
        BaseWeather baseWeather = baseWeatherService.selectBaseWeatherByDayOccur(dayOccur);
        mmap.put("baseWeather", baseWeather);
        return prefix + "/edit";
    }

    /**
     * 修改保存天气
     */
    @Log(title = "天气", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseWeather baseWeather)
    {
        return toAjax(baseWeatherService.updateBaseWeather(baseWeather));
    }

    /**
     * 删除天气
     */
    @Log(title = "天气", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baseWeatherService.deleteBaseWeatherByDayOccurs(ids));
    }
}
